package com.gopay.common.cps.manager.gsrt.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.common.cps.dao.gsrt.GsrtCustSignedDao;
import com.gopay.common.cps.manager.gsrt.GsrtCustSignedQueryManager;
import com.gopay.common.domain.gsrt.GsrtCustSigned;

@Service("gsrtCustSignedQueryManager")
public class GsrtCustSignedQueryManagerImpl implements GsrtCustSignedQueryManager {

    @Resource(name = "gsrtCustSignedDao")
    private GsrtCustSignedDao gsrtCustSignedDao;
    
    private final String FIND_TIMEOUT_ORDER_BY_TIMESPAN_SQL = "SELECT  c.id  FROM GSRT_CUST_SIGNED C "
			+ "  where  (C.signed_state='05' "
			+ " AND ROUND(TO_NUMBER(SYSDATE - (C.CREATED_TS+0))*24*60) >= :timeSpan) ";
    

    /**
     * 根据商户MerCustId查询投资者的信息
     * @param merCustId
     * @param ConstracterId
     * @return
     */
    @Override
    public List<GsrtCustSigned> getlistByConstracterId(String merCustId, String constracterId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("merCustId", merCustId);
        map.put("constracterId", constracterId);
        String hql = " from GsrtCustSigned where merCustId=:merCustId and constracterId=:constracterId   ";
        List<GsrtCustSigned> list = gsrtCustSignedDao.findByHql(hql, map);
        return list;
    }

    @Override
    public boolean isSign(String custId, String merCustId) {
        GsrtCustSigned gsrtCustSigned = gsrtCustSignedDao.findByCustIdAndMerCustId(custId, merCustId);
        if (gsrtCustSigned != null) {
            return true;
        }
        return false;
    }
    
    @Override
    public GsrtCustSigned getByGopayOrderId(String gopayOrderId) {
        return gsrtCustSignedDao.findByGopayOrderId(gopayOrderId);
    }
    @Override
    public GsrtCustSigned findByMerOrderId(String merOrderId) {
    	  return gsrtCustSignedDao.findByMerOrderId(merOrderId);
    }

    /**
     * 根据用户CustId查询投资者的信息
     * 
     * @param merCustId
     * @param ConstracterId
     * @return
     */
    @Override
    public GsrtCustSigned getlistByCustId(String custId) {
        String hql = "from GsrtCustSigned where custId = :custId ";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("custId", custId);
        List<GsrtCustSigned> userInfoList = gsrtCustSignedDao.findByHql(hql, param);
        if (userInfoList == null || userInfoList.size() == 0) {
            return null;
        }
        return userInfoList.get(0);

    }
    @Override
    public GsrtCustSigned getlistByCustId(String merCustId, String merOrderId) {
    	String hql = "from GsrtCustSigned where merCustId = :merCustId  and merOrderId=:merOrderId";
    	Map<String, Object> param = new HashMap<String, Object>();
    	param.put("merCustId", merCustId);
    	param.put("merOrderId", merOrderId);
    	List<GsrtCustSigned> userInfoList = gsrtCustSignedDao.findByHql(hql, param);
    	if (userInfoList == null || userInfoList.size() == 0) {
    		return null;
    	}
    	return userInfoList.get(0);
    	
    }
	/**
	 * 根据用户CustId查询投资者的信息
	 * @param merCustId
	 * @param ConstracterId
	 * @return
	 */
	@Override
    public List<GsrtCustSigned> getListByCustId(String CustId){
		   String hql = "from GsrtCustSigned where custId = :custId ";
	        Map<String, Object> param = new HashMap<String, Object>();
	        param.put("custId", CustId);
	        List<GsrtCustSigned> list = gsrtCustSignedDao.findByHql(hql, param);
	        return list;
	}

    @Override
    public void update(GsrtCustSigned en) {
        gsrtCustSignedDao.updateGsrtCustSigned(en);

    }

    @Override
    public GsrtCustSigned getByMerCustIdAndConstracterId(String merCustId, String constracterId) {
        GsrtCustSigned gsrtCustSigned = gsrtCustSignedDao.findByMerCustIdAndConstracterId(merCustId, constracterId);
        return gsrtCustSigned;
    }

    @Override
    public boolean getFailByCustIdAndMerCustId(String custId, String merCustId) {
        return gsrtCustSignedDao.findFailByCustIdAndMerCustId(custId, merCustId);
    }

	@Override
	public List<Object> findTimeoutListByTimeSpan(int timeSpan) {
		Map m = new HashMap();
		m.put("timeSpan", timeSpan);
		List<?> result = gsrtCustSignedDao.findBySql(FIND_TIMEOUT_ORDER_BY_TIMESPAN_SQL, m);
		return (List<Object>) result;
	}

}
